package com.example.myapplication;

import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

import static android.content.ContentValues.TAG;

/**
 * Created by lyt on 2020/12/6 0006.
 */

public class JdbcUtil {
    private static JdbcUtil instance;

    public static JdbcUtil getInstance(){
        if (instance ==null){
            instance = new JdbcUtil();
        }
        return instance;
    }

    public Connection getConnection(String dbName,String name,String password) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
//            Class.forName("com.mysql.cj.jdbc.Driver");

            String url = "jdbc:mysql://10.0.2.2:3306/"+dbName;
            Connection conn = DriverManager.getConnection(url,name,password);
            return conn;
        } catch (Exception e) {
            Log.d(TAG,e.toString());
            return null;
        }
    }

    public Connection getConnection(String file){
        File f = new File(file);
        if(!f.exists()){
            return null;
        }else {
            Properties pro = new Properties();
            try {
                Class.forName("com.mysql.jdbc.Driver");
                pro.load(new FileInputStream(f));
                String url = pro.getProperty("url");
                String username = pro.getProperty("username");
                String password = pro.getProperty("password");
                return DriverManager.getConnection(url,username,password);
            }catch (Exception e){
                return null;
            }
        }
    }
}
